/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.ticket;

import java.io.File;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.boxin.framework.base.utils.Constants;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.momarketing.service.QRCodeService;
import com.boxin.ims.modules.wechat.entity.ticket.Ticket;
import com.boxin.ims.modules.wechat.dao.ticket.TicketDao;

/**
 * 入场券Service
 * @author Jakemanse
 * @version 2014-01-28
 */
@Component
@Transactional(readOnly = true)
public class TicketService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(TicketService.class);
	
	@Autowired
	private QRCodeService qrCodeService;
	
	@Autowired
	private TicketDao ticketDao;
	
	public Ticket get(Long id) {
		return ticketDao.findOne(id);
	}
	
	
	public List<Ticket> find(Ticket ticket){
		DetachedCriteria dc = ticketDao.createDetachedCriteria();
		
		if (ticket.getUser()!=null && ticket.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", ticket.getUser().getId()));
		}
		
		if (ticket.getMember()!=null && ticket.getMember().getId()>0){
			dc.createAlias("member", "member");
			dc.add(Restrictions.eq("member.id", ticket.getUser().getId()));
		}
		
		
		if (StringUtils.isNotEmpty(ticket.getName())){
			dc.add(Restrictions.like("name", "%"+ticket.getName()+"%"));
		}
		
		dc.addOrder(Order.desc("id"));
		return ticketDao.find( dc);
	}
	
	public Page<Ticket> find(Page<Ticket> page, Ticket ticket) {
		DetachedCriteria dc = ticketDao.createDetachedCriteria();
		if (ticket.getUser()!=null && ticket.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", ticket.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(ticket.getName())){
			dc.add(Restrictions.like("name", "%"+ticket.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return ticketDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(Ticket ticket) {
		
		//生成二维码
		QRCode qrcode = new QRCode();
		String content = QRCodeUtils.generateTicketSN(ticket.getUser().getId());
		String path = QRCodeUtils.generateDefaultQRCodePNG(content);
		qrcode.setFileName(content+".png");
		qrcode.setPngPath(path+".png");
		qrcode.setContent(content);
		qrCodeService.save(qrcode);	//保存二维码	
		ticket.setQrCode(qrcode);
		
		ticketDao.save(ticket);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		ticketDao.deleteById(id);
	}
	
}
